class Solution {
    public int romanToInt(String s) {
        int sum = 0 ;
        for(int i = 0 ; i<s.length()  ; i++){
            char tmp = s.charAt(i) ;
            if(i < s.length() - 1){
                char tmp2 = s.charAt(i+1) ;
                if(returnJian(tmp , tmp2)){
                    sum += returnNum(tmp2) - returnNum(tmp) ;
                    i++ ;
                }else{
                    sum += returnNum(tmp);
                }

            }else{
                sum += returnNum(tmp);
            }
        }

        return sum ;
    }

    public boolean returnJian(char ch1 , char ch2) {
        switch (ch1) {
            case 'I': {
                if (ch2 == 'V' || ch2 == 'X') {
                    return true;
                } else {
                    return false;
                }
            }
            case 'X': {
                if (ch2 == 'L' || ch2 == 'C') {
                    return true;
                } else {
                    return false;
                }
            }
            case 'C': {
                if (ch2 == 'D' || ch2 == 'M') {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return  false ;
    }

    public int returnNum(char ch){
        switch(ch){
            case 'I': return 1 ;

            case 'V':return 5 ;

            case 'X':return 10 ;

            case 'L':return 50 ;
            case 'C':return 100 ;
            case 'D':return 500 ;

            case 'M':return 1000 ;
        }
        return -1;
    }


}